package com.cxyzy.donwloadmanagerdemo

import android.Manifest
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.qw.soul.permission.SoulPermission
import com.qw.soul.permission.bean.Permission
import com.qw.soul.permission.callbcak.CheckRequestPermissionListener
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        button.setOnClickListener { downloadApkWithPermission() }
    }

    private fun downloadApkWithPermission() {
        SoulPermission.getInstance().checkAndRequestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                object : CheckRequestPermissionListener {
                    override fun onPermissionOk(permission: Permission) {
                        downloadApk()
                    }

                    override fun onPermissionDenied(permission: Permission?) {}
                })
    }

    private fun downloadApk() {
        val url = "https://gitee.com/cxyzy1/donwloadManagerDemo/raw/master/test-app.apk"
        val name = "test-app.apk"
        DownloadUtils(this).downloadAPK(url, name)
    }
}